package importExport;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;


public class SimpleXmlOut {
	
	Document doc;
	 
	 public static Document createDoc(File cFile) {
			Document document = null;
			try {
				DocumentBuilderFactory factory = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder builder;

				builder = factory.newDocumentBuilder();

				document = builder.parse(cFile);
			} catch (SAXParseException spe) {
				System.out.println("\n** Parsing error, line "
						+ spe.getLineNumber() + ", uri " + spe.getSystemId());
				System.out.println("   " + spe.getMessage());
				Exception e = (spe.getException() != null) ? spe.getException()
						: spe;
				e.printStackTrace();
			} catch (SAXException sxe) {
				Exception e = (sxe.getException() != null) ? sxe.getException()
						: sxe;
				e.printStackTrace();
			} catch (ParserConfigurationException pce) {
				pce.printStackTrace();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
			return document;
		}
	public static Document createDom(){
		Document cDoc=null;
		DocumentBuilderFactory docBFac;
		DocumentBuilder docBuild;
		try
		{
		docBFac = DocumentBuilderFactory.newInstance();
		docBuild = docBFac.newDocumentBuilder();
		
		cDoc = docBuild.newDocument();
		}
		catch( Exception e )
		{
		System.out.println("Dokument konnte nicht erzeugt werden");
		}
		return cDoc;
	}
	
	
	// This method writes a DOM document to a file
	public static void writeXmlFile(Document doc, File file) {
		if (file != null) {
			try {
				// Prepare the DOM document for writing
				Source source = new DOMSource(doc);

				// Prepare the output file

				Result result = new StreamResult(file);

				// Write the DOM document to the file
				Transformer xformer = TransformerFactory.newInstance()
						.newTransformer();

				xformer.transform(source, result);

			} catch (TransformerConfigurationException e) {
			} catch (TransformerException e) {
			}
		}

	}
	  
	  
	  public static void printDOMTree(Node node,PrintStream p) 
	  {
	    int type = node.getNodeType();
	    switch (type)
	    {
	      // print the document element
	      case Node.DOCUMENT_NODE: 
	        {
	          printDOMTree(((Document)node).getDocumentElement(),p);
	          break;
	        }

	        // print element with attributes
	      case Node.ELEMENT_NODE: 
	        {
	          p.print("<");
	           p.print(node.getNodeName());
	          NamedNodeMap attrs = node.getAttributes();
	          for (int i = 0; i < attrs.getLength(); i++)
	          {
	            Node attr = attrs.item(i);
	            p.print(" " + attr.getNodeName() + 
	                      "=\"" + attr.getNodeValue() + 
	                      "\"");
	          }
	          p.print(">");

	          NodeList children = node.getChildNodes();
	          if (children != null)
	          {
	            int len = children.getLength();
	            for (int i = 0; i < len; i++)
	              printDOMTree(children.item(i),p);
	          }

	          break;
	        }

	        // handle entity reference nodes
	      case Node.ENTITY_REFERENCE_NODE: 
	        {
	         p.print("&");
	          p.print(node.getNodeName());
	          p.println(";");
	          break;
	        }

	        // print cdata sections
	      case Node.CDATA_SECTION_NODE: 
	        {
	          p.print("<![CDATA[");
	          p.print(node.getNodeValue());
	          p.println("]]>");
	          break;
	        }

	        // print text
	      case Node.TEXT_NODE: 
	        {
	          p.print(node.getNodeValue());
	          break;
	        }

	        // print processing instruction
	      case Node.PROCESSING_INSTRUCTION_NODE: 
	        {
	          p.println("<?");
	         p.print(node.getNodeName());
	          String data = node.getNodeValue();
	          {
	            p.print(" ");
	           p.print(data);
	          }
	         p.println("?>");
	          break;
	        }
	    }

	    if (type == Node.ELEMENT_NODE)
	    {
	     p.print("</");
	     p.print(node.getNodeName());
	     p.println('>');
	    }
	  } // DOMTree(Node, PrintWriter)
}
